package presenter;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author alamarre
 */
public class PresenterServer implements Runnable {
    ServerSocket socket0;
    Socket socket1;
    
    SimpleVariableFile svf;
    DataOutputStream out;
    MainWindow mw;
    
    public PresenterServer(Socket socket, MainWindow mw) {
        socket1=socket;
        this.mw=mw;
    }
    public PresenterServer(MainWindow mw) {
        svf=new SimpleVariableFile("MediaData.txt");
        this.mw=mw;
        try {
            if(svf.exists("PortNumber")) {
                int port=Integer.parseInt(svf.getVariable("PortNumber"));
                port=12345;
                socket0=new ServerSocket(port);
            } else {
                socket0=new ServerSocket(12345);
            }
            while(true) {
            new Thread(new PresenterServer(socket0.accept(),mw)).run();}
            
        } catch(Exception e) {
           
       }
            
            
    }
    
    public void doAction(String input) {
        System.out.println(input);
        try {
            if(input.startsWith("update")) {
               String info=input.substring("update".length()+1);
               if(info.startsWith("0")) {
                   if (mw.sd0.getSong().getNumber()==Integer.parseInt(info.substring(2)))
                       out.writeUTF("f");
                   else
                       out.writeUTF("t");          
               } else if(mw.sd1.getSong().getNumber()==Integer.parseInt(info.substring(2))) {
                       out.writeUTF("f");
               } else {
                       out.writeUTF("t");
               }

            }
            
            if(input.startsWith("activate")) {
                String id=input.substring("activate".length()+1);
                if(id.startsWith("sd0")) {
                   mw.sd0.setActive(Integer.parseInt(id.substring("sd0".length()+1)));
                   mw.sd0.update=true;
                } else if(id.startsWith("sd1")) {
                   mw.sd1.setActive(Integer.parseInt(id.substring("sd1".length()+1)));
                   mw.sd1.update=true;
                }
            }
            
            if(input.startsWith("getActive")) {
                String id=input.substring("getActive".length()+1);
                if(id.startsWith("sd0")) {
                   out.writeUTF(""+mw.sd0.getActive());
                   
                } else if(id.startsWith("sd1")) {
                   out.writeUTF(""+mw.sd1.getActive());
                }
            }
            
            if(input.startsWith("getLimit")) {
                String id=input.substring("getLimit".length()+1);
                if(id.startsWith("sd0")) {
                   out.writeUTF(""+(mw.sd0.verses.length-1));
                   
                } else if(id.startsWith("sd1")) {
                   out.writeUTF(""+(mw.sd1.verses.length-1));
                }
            }
            
            if(input.startsWith("getSong")) {
                try {
                    if(input.substring("getSong".length()+1).equals("0"))
                        sendSong(mw.sd0);
                    else
                        sendSong(mw.sd1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            
            if(input.startsWith("getVerse")) {
                try {
                    input=input.substring("getSong".length()+2);
                    if(input.substring(0, 1).equals("0")){
                        int verse= Integer.parseInt(input.substring(2));
                        sendVerse(mw.sd0,verse);
                        
                    } else {
                        System.out.println(input+" "+input.substring(2));
                        int verse= Integer.parseInt(input.substring(2));
                        sendVerse(mw.sd1,verse);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    
    public void sendSong(SongDisplayer sd) {
        sendVerse(sd,sd.getActive());
    }
    
    public void sendVerse(SongDisplayer sd, int verse) {
        SongFile sf=sd.getSong();
        SongVerse sv=sf.getVerse(verse);
        try {
            
                String[] lines=sv.getLyrics();
                out.writeUTF(sv.getType());
                for(int j=0; j<lines.length; j++) {
                    out.writeUTF(lines[j]);
                    System.out.println(lines[j]);
                }
                out.writeUTF("EOV");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main (String[] args) {
        new PresenterServer(new MainWindow());
    }

    public void run() {
        try{
        out = new DataOutputStream(socket1.getOutputStream());
            DataInputStream in=new DataInputStream((socket1.getInputStream()));
            System.out.println("client connected");
            
            String x="";
            while(!x.equals("close")) {
               System.out.println("reading action");
                x=in.readUTF();
                System.out.println("doing action "+x);
                doAction(x);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
